package com.ddwl.user.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.user.QueryUserLevelBo;
import com.ddwl.schema.bo.user.UserLevelBo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.user.UserLevelVo;
import com.ddwl.user.service.UserLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Fu Zhaohui
 * @date 2016/12/14
 */
@Api(tags = "用户等级")
@RequestMapping("userLevel")
@RestController
public class UserLevelController extends BaseController {

    @Autowired
    private UserLevelService userLevelService;

    @PostMapping("/saveUserLevel")
    @ApiOperation("保存用户等级")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo saveUserLevel(@RequestBody UserLevelBo bo) {
        bo.setCreatedUserId(getUserId());
        userLevelService.saveUserLevel(bo);
        return RestVo.ok();
    }

    @PostMapping("/updateUserLevel")
    @ApiOperation("更新用户等级")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo updateUserLevel(@RequestBody UserLevelBo bo) {
        bo.setModifiedUserId(getUserId());
        userLevelService.updateUserLevel(bo);
        return RestVo.ok();
    }

    @DeleteMapping("/deleteUserLevel")
    @ApiOperation("删除用户等级")
    public RestVo delete(@RequestParam String id) {
        userLevelService.delete(id, getUserId());
        return RestVo.ok();
    }

    @ApiOperation("用户等级列表")
    @PostMapping("/queryUserLevel")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<List<UserLevelVo>> queryUserLevel(@RequestBody QueryUserLevelBo bo) {
        return RestVo.ok(userLevelService.getList(bo));
    }

    @ApiOperation("用户当前及下一等级")
    @GetMapping("/queryNextLevel")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<List<UserLevelVo>> queryNextLevel() {
        return RestVo.ok(userLevelService.getNextLevel(getUserId()));
    }

    @ApiOperation("用户当前及下一等级")
    @GetMapping("/queryNextLevel/{userId}")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<List<UserLevelVo>> queryNextLevel(@PathVariable String userId) {
        return RestVo.ok(userLevelService.getNextLevel(getUserId()));
    }

    @ApiOperation("根据用户等级编码获取用户登记信息")
    @GetMapping("/getByUserLevelCode")
    @Auth(product = {BaseEnum.ProductEnum.API})
    public RestVo<UserLevelVo> getByUserLevelCode(@RequestParam Integer code) {
        return RestVo.ok(userLevelService.getByUserLevelCode(code));
    }

    @ApiOperation("订正用户等级")
    @PostMapping("/correctUserLevel")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo correctUserLevel() {
        userLevelService.correctUserLevel();
        return RestVo.ok();
    }

    @PostMapping("/changeId")
    @ApiOperation("把id反转成时间戳")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo changeId() {
        userLevelService.changeId();
        return RestVo.ok();
    }


}
